package com.mrjy.queries.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mrjy.common.dto.MyMessagesDto;
import com.mrjy.common.entity.QuestionComment;
import com.mrjy.common.entity.QuestionInfo;
import com.mrjy.common.entity.RecordInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.enums.ResultEnum;
import com.mrjy.common.exception.InnerException;
import com.mrjy.common.util.GlobalConstant;
import com.mrjy.queries.bo.IQuestionCommentBo;
import com.mrjy.queries.dto.PayQuestionCommentParamDTO;

@Controller
@RequestMapping("/queries/questionComment")
public class QuestionCommentController {

	@Resource
	private IQuestionCommentBo commentBo;
	
	/**
	 * 分页获取评论
	 * 个人中心--我的问题--根据问题id 获取详细评论列表
	 * @param comment comment.questionId 问题id
	 * @param session
	 * @author csshui isUser=true
	 * @return
	 */
	@RequestMapping("/getComments")
	@ResponseBody
	public List<QuestionComment> getComments(QuestionComment comment,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return commentBo.queryComments(comment,userInfo);
	}
	
	/**
	 * TODO 没有进行事务处理
	 * 评论问题
	 * @param comment---评论的问题id,回复人的id comment.questionId(问题id) comment.commentContent(评论内容)
	 * @param session
	 * @return
	 */
	@RequestMapping("/doComment")
	@ResponseBody
	public String doComment(QuestionComment comment,HttpSession session){
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return commentBo.doComment(comment, userInfo);
	}
	
	
//	/**
//	 * 读取通知，将消息标志已读
//	 * @param session
//	 * @return
//	 */
//	@RequestMapping("/readNotice")
//	@ResponseBody
//	public String readNotice(HttpSession session){
//		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
//		return commentBo.readNotice(userInfo);
//	}
	
	/**
	 * 通过userId查询其评论数
	 * @param userId 用户id（老师id）
	 * @return
	 */
	@RequestMapping("/queryCommentListByUserId")
	@ResponseBody
	public Object queryCommentListByUserId(QuestionInfo info){
		return commentBo.queryCommentListByUserId(info);
	}

	/**
	 * 通过questionId查询其一级评论数
	 * @param questionId 
	 * @return
	 */
	@RequestMapping("/selectCommentByQuestionId")
	@ResponseBody
	public Object selectCommentByQuestionId(QuestionComment info,HttpSession session){
		UserInfo userInfo = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO);
		if (userInfo != null) {
			info.setUserId(userInfo.getId());
		}
		return commentBo.selectCommentByQuestionId(info);
	}
	/**
	 * 通过quoteCommentId查询二级评论
	 * @param quoteCommentId 
	 * @return
	 */
	@RequestMapping("/selectSecondCommentByQuoteCommentId")
	@ResponseBody
	public Object selectSecondCommentByQuoteCommentId(QuestionComment info){
		return commentBo.selectSecondCommentByQuoteCommentId(info);
	}
	
	/**
	 * 对问题进行评论
	 * @param userId 评论人id
	 * @param questionId 问题id
	 * @param commentId 被提问人id
	 * @param commentContent 评论内容
	 * @param quoteCommentId 二级评论id
	 * @param status 品论或回复
	 * @return
	 */
	@RequestMapping("/toComment")
	@ResponseBody
    public Object toComment(QuestionComment qComment, RecordInfo recordInfo) {
        return commentBo.txToComment(qComment, recordInfo);
    }

	/**
	 * 通过id查询记录
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryById")
	@ResponseBody
	public Object queryById(Integer id){
		return commentBo.queryById(id);
	}
	/**
	 * 根据点赞数，评论数查看最热分页评论
	 * @param comment
	 * @return
	 * @author Javin : isUer = true,
	 */
	@RequestMapping("/queryPageCommentsOrderByNum")
	@ResponseBody
	public Object queryPageCommentsOrderByNum(QuestionComment comment){
		return commentBo.queryPageCommentsOrderByNum(comment);
	}
	/**
	 * 根据点赞数，评论数查看最热分页评论
	 * 优先显示最近3天
	 * 其次显示最近30天
	 * 再次显示历史获赞最多
	 * @param comment
	 * @return
	 * @author hfei : isUer = true,
	 */
	@RequestMapping("/queryPageCommentsHot")
	@ResponseBody
	public Object queryPageCommentsHot(QuestionComment comment, HttpSession session){
		return commentBo.queryPageCommentsHot(comment,session);
	}
	/**
	 * 根据userId查询我的回答
	 * @param comment.userId
	 * @return
	 * @author csshui
	 */
	@RequestMapping("/queryMyQAPageByUserId")
	@ResponseBody
    public Object queryMyQAPageByUserId(QuestionComment comment, HttpSession session) {
        return commentBo.queryMyQAPageByUserId(comment, session);
    }

	/**
	 * 根据被评论人Id查询我的消息--问答
	 * @param dto.commentedId 被评论人Id
	 * @return
	 * @author csshui
	 */
	@RequestMapping("/queryMyMessagesPageByCommentedId")
	@ResponseBody
	public Object queryMyMessagesPageByCommentedId(MyMessagesDto dto){
		return commentBo.queryMyMessagesPageByCommentedId(dto);
		
	}
	/**
	 * 根据Id更新未读状态为已读
	 * @param id
	 * @return
	 * @author csshui
	 */
	@RequestMapping("/updateReadFlagToReaded")
	@ResponseBody
	public Object updateReadFlagToReaded(Integer id){
		return commentBo.txUpdateReadFlagToReaded(id);
	}
	
	/**
	 * 用户根据问题id查看自己是否已经回答过该问题
	 * @param comment
	 * @return
	 * @author Javin
	 */
	@RequestMapping("/queryCommentByQid")
	@ResponseBody
	public Object queryCommentByQid(QuestionComment comment){
		return commentBo.queryCommentByQid(comment);
	}
	
	/**
	 * 根据评论ID跟新评论内容
	 * @param comment
	 * @return
	 * @author Javin
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	public Object updateById(QuestionComment comment,HttpSession session){
		return commentBo.txUpdateById(comment,session);
	}
	/**
	 * 禁用、起用
	 * @param ids
	 * @return
	 */
	@RequestMapping("/updateDisableFlag")
	@ResponseBody
	public Object updateDisableFlag(QuestionComment comment,HttpSession session) {
		UserInfo userInfo = ((UserInfo) session.getAttribute(GlobalConstant.USER_INFO));
		return commentBo.txUpdateDisableFlag(comment,userInfo);
	}
	/**
	 * 后台获取列表 
	 * @param info
	 * @return
	 */
	@RequestMapping("/selectQuestionCommentListPage")
	@ResponseBody
	public Object selectQuestionCommentListPage(QuestionComment info,HttpSession session){
		return commentBo.selectQuestionCommentListPage(info,session);
	}
	
	
	/**
	 * 导出excel
	 * 
	 * @param info
	 * @return
	 */
	@RequestMapping("/export")
	@ResponseBody
	public Object export(HttpServletResponse response,QuestionComment info,HttpSession session) {
		return commentBo.exportExl(info,response,session);
	}
	
    /**
     * 获取问题回答语音列表
     *
     * @param info
     * @return
     */
    @RequestMapping("/queryPageCommentWithRecord")
    @ResponseBody
    public Object queryPageCommentWithRecord(QuestionComment info) {
        return commentBo.queryPageCommentWithRecord(info);
    }

    /**
     * 对问题进行语音回答
     *
     * @return
     */
    @RequestMapping("/toCommentByRecord")
    @ResponseBody
    public Object toCommentByRecord(QuestionComment qComment, RecordInfo recordInfo) {
        return commentBo.txToComment(qComment, recordInfo);
    }

    /**
     * 对问题进行语音回答
     *
     * @return
     */
    @RequestMapping("/listenRecord")
    @ResponseBody
    public Object listenRecord(QuestionComment qComment) {
        return commentBo.listenRecord(qComment);
    }

    /**
     * 根据userId查处好评总数
     *
     * @return
     */
    @RequestMapping("/querySumGoodNumByUserId")
    @ResponseBody
    public Object querySumGoodNumByUserId(QuestionComment comment, HttpSession session) {
        return commentBo.querySumGoodNumByUserId(comment, session);
    }

    /**
     * 根据userId 和  question_id 查询导师回答详情
     *
     * @return
     */
    @RequestMapping("/queryTeacherCommentInfoByQidUid")
    @ResponseBody
    public Object queryTeacherCommentInfoByQidUid(QuestionComment comment) {
        return commentBo.queryTeacherCommentInfoByQidUid(comment);
    }

    /**
     * 值得你听
     *
     * @return
     */
    @RequestMapping("/queryDeserveToListen")
    @ResponseBody
    public Object queryDeserveToListen(QuestionComment comment) {
        return commentBo.queryDeserveToListen(comment);
    }

    @RequestMapping("/addCommentNum")
    @ResponseBody
    public Object addCommentNum(Integer id) {
        return commentBo.addCommentNum(id);
    }

    /**
     * 根据点赞数，评论数查看最热分页评论
     * 优先显示最近3天
     * 其次显示最近30天
     * 再次显示历史获赞最多
     *
     * @param comment
     * @return
     * @author lyon : isUer = true,
     */
    @RequestMapping("/queryPageTopicCommentsHot")
    @ResponseBody
    public Object queryPageTopicCommentsHot(QuestionComment comment, HttpSession session) {
        return commentBo.queryPageTopicCommentsHot(comment, session);
    }
    
    /**
     * 微信1.6.8 顾问+榜样+家园+收获
     * 渲染 顾问-付费动态
     * @param comment sortType:mine--我的  new--最新  hot--最热
     * @param session
     * @return
     */
    @RequestMapping("/drawConsultant")
    @ResponseBody
    public Object drawConsultant(QuestionComment comment, HttpSession session) {
        return commentBo.drawConsultant(comment, session);
    }
    
    /**
     * 微信1.6.8 顾问+榜样+家园+收获
     * 渲染 家园-帖子动态
     * @param comment
     * @param session
     * @return
     */
    @RequestMapping("/drawHome")
    @ResponseBody
    public Object drawHome(QuestionComment comment, HttpSession session) {
        return commentBo.drawHome(comment, session);
    }
    
    /**
     * 1.6.9.2 
     * 家园-活动课程
     * @param comment
     * @param session
     * @return
     */
//    @RequestMapping("/home/activity/list")
//    @ResponseBody
//    public Object list(@RequestParam Integer page
//    		,@RequestParam Integer rows, HttpSession session) {
//        return commentBo.queryActivityPage(page,rows,session);
//    }
    

    /**
     * 显示全站最新的回帖内容及其发布者，来源帖子标题
     *
     * @param info
     * @return
     */
    @RequestMapping("/quesyNewTextComment")
    @ResponseBody
    public Object quesyNewTextComment(QuestionInfo info) {
        return commentBo.quesyNewTextComment(info);
    }
    /**
     * 通过问题id查询一级评论数
     * @param questionId 问题id
     * @return
     */
    @RequestMapping("/firstCommentNumCount")
    @ResponseBody
    public Object firstCommentNumCount(Integer questionId){
    	return commentBo.queryFirstCommentNumByQuestionId(questionId) ;
    }
    
    @RequestMapping("/adviser/newComment")
    @ResponseBody
    public Object queryQuesctionContentByTypeIds(){
    	return commentBo.queryQuesctionContentByTypeIds() ;
    }
    
    @RequestMapping("/adviser/payQuestionComment")
    @ResponseBody
    public Object queryPayQuestionComment(PayQuestionCommentParamDTO dto , HttpSession session){
    	UserInfo ui = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO) ;
    	if(ui == null){
    		throw new InnerException(ResultEnum.NOT_LOGIN);
    	}
    	dto.setUserId(ui.getId());
    	return commentBo.queryPayQuestionComment(dto) ;
    }
    
    @RequestMapping("/adviser/payQuestionCommentCount")
    @ResponseBody
    public Object queryCountPayQuestionComment(PayQuestionCommentParamDTO dto , HttpSession session){
    	UserInfo ui = (UserInfo) session.getAttribute(GlobalConstant.USER_INFO) ;
    	if(ui == null){
    		throw new InnerException(ResultEnum.NOT_LOGIN);
    	}
    	return commentBo.queryCountPayQuestionComment(dto) ;
    }
    
}
